package operation;

import book.Book;
import book.BookShelf;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void working(BookShelf bookShelf) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除图书的名称：");
        String name = scanner.next();

        int index = 0;
        int i=0;
        int currentSize = bookShelf.getUsedSize();
        for (; i < currentSize; i++) {
            Book book = bookShelf.getBooks(i);
            if(book.getName().equals(name)){
                index = i;
                break;
            }
        }

        if(i >= currentSize){
            System.out.println("未找到这本书");
            return;
        }else {
            for (int j = index; j < currentSize; j++) {
                Book book = bookShelf.getBooks(j + 1);
                bookShelf.setBooks(book, j);
            }
        }
        bookShelf.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}
